package zmee.pur.src.opplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;

import java.math.BigDecimal;

/**
 * @Description: 供应商协同云-招标管理-项目立项-招标立项申请单
 * @Author: 何朗
 * @Date: 2023/11/16
 */
public class DemandOpPlugin extends AbstractOperationServicePlugIn {

    final static String SUBMIT = "submit";

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        e.addValidator(new AbstractValidator() {
            @Override
            public void validate() {
                String key = this.getOperateKey();
                if (SUBMIT.equals(key)) {
                    ExtendedDataEntity[] dataEntities = this.getDataEntities();
                    for (int i = 0; i < dataEntities.length; i++) {
                        DynamicObject dataEntity = dataEntities[i].getDataEntity();
                        Object pkValue = dataEntity.getDynamicObject("pro_approval").getPkValue();
                        DynamicObject outBudget = QueryServiceHelper.queryOne("pmas_outbudget", "zmee_textfield,project.id",
                                new QFilter("id", QCP.equals, pkValue).toArray());
                        DynamicObject proApproval = QueryServiceHelper.queryOne("pmas_pro_approval", "projcetbugamt",
                                new QFilter("pro", QCP.equals, outBudget.getLong("project.id")).toArray());
                        // 2023年11月16日 12:28:38 开发何朗 实施江玲 新增需求 校验
                        // 项目支出预算单-项目概算总额与项目立项单-项目概算总额不一致，请调整支出预算单项目概算总额！
                        /**
                         * 1.获取项目立项单中的年度项目预算
                         * 2.获取项目立项单中的项目名称中的项目概算（元）
                         */
                        BigDecimal totalProjectEstimate = outBudget.getBigDecimal("zmee_textfield");
                        BigDecimal projectBugAmt = proApproval.getBigDecimal("projcetbugamt");
                        if (totalProjectEstimate.compareTo(projectBugAmt) != 0) {
                            this.addMessage(dataEntities[i], "项目支出预算单-项目概算总额与项目立项单-项目概算总额不一致，请调整支出预算单项目概算总额！");
                        }
                    }
                }
            }
        });
    }
}